// profile.js
Page({
  data: {
    hasUserInfo: false,
    userInfo: {
      avatarUrl: '',
      nickName: ''
    },
    creditScore: 100,
    currentTab: 'ongoing',
    ongoingOrders: [],
    historyOrders: [],
    userId: ''
  },

  onLoad() {
    this.loadUserInfo();
    this.loadOrders();
  },

  onShow() {
    // 每次显示页面时重新加载订单数据
    this.loadOrders();
  },

  loadUserInfo() {
    // 加载用户信息
    const userInfo = wx.getStorageSync('userInfo') || {};
    const userId = wx.getStorageSync('userId') || '';
    
    // 检查是否有用户信息
    const hasUserInfo = userInfo && userInfo.nickName;
    
    this.setData({
      userInfo,
      userId,
      hasUserInfo
    });
    
    console.log('加载用户信息:', userInfo, '登录状态:', hasUserInfo);
  },

  loadOrders() {
    // 加载订单数据
    const ongoingOrders = wx.getStorageSync('ongoingOrders') || [];
    const historyOrders = wx.getStorageSync('historyOrders') || [];
    this.setData({
      ongoingOrders,
      historyOrders
    });
  },

  checkLoginStatus() {
    // 模拟检查登录状态
    const userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      this.setData({
        hasUserInfo: true,
        userInfo: userInfo
      });
    }
  },

  onLoginTap() {
    wx.getUserProfile({
      desc: '用于完善用户资料',
      success: (res) => {
        const userInfo = res.userInfo;
        wx.setStorageSync('userInfo', userInfo);
        this.setData({
          hasUserInfo: true,
          userInfo: userInfo
        });
      }
    });
  },

  switchTab(e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({
      currentTab: tab
    });
  },

  returnEquipment(e) {
    const { id } = e.currentTarget.dataset;
    wx.showModal({
      title: '确认归还',
      content: '确定要归还该装备吗？',
      success: (res) => {
        if (res.confirm) {
          // 处理归还逻辑
          const ongoingOrders = this.data.ongoingOrders.filter(order => order.id !== id);
          const returnedOrder = this.data.ongoingOrders.find(order => order.id === id);
          
          if (returnedOrder) {
            // 更新订单状态
            returnedOrder.status = 'completed';
            returnedOrder.actualReturnTime = new Date().toLocaleString();
            
            // 更新历史订单
            const historyOrders = [returnedOrder, ...this.data.historyOrders];
            
            // 保存到本地存储
            wx.setStorageSync('ongoingOrders', ongoingOrders);
            wx.setStorageSync('historyOrders', historyOrders);
            
            // 更新页面数据
            this.setData({
              ongoingOrders,
              historyOrders
            });

            // 显示成功提示
            wx.showToast({
              title: '归还成功',
              icon: 'success'
            });
          }
        }
      }
    });
  },

  onChooseAvatar(e) {
    const { avatarUrl } = e.detail;
    const userInfo = this.data.userInfo;
    userInfo.avatarUrl = avatarUrl;
    this.setData({
      userInfo
    });
    wx.setStorageSync('userInfo', userInfo);
  },

  onLogoutTap() {
    wx.showModal({
      title: '确认退出',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          // 清除用户信息
          wx.removeStorageSync('userInfo');
          wx.removeStorageSync('userId');
          
          // 更新状态
          this.setData({
            hasUserInfo: false,
            userInfo: {},
            userId: '',
            creditScore: 100
          });
          
          wx.showToast({
            title: '已退出登录',
            icon: 'success'
          });
        }
      }
    });
  }
}); 